package com.tjtt.gwm.action;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.tjtt.gwm.bean.Junyansxm;
import com.tjtt.gwm.service.EquipmentService;
import com.tjtt.gwm.service.JunyansService;
import com.tjtt.tdm.base.BaseAction;
import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.base.StringUtil;
import com.tjtt.tdm.codeReview.service.ICodeReviewService;
import com.tjtt.tdm.util.PubMethod;

public class JunyansItemAction extends BaseAction {
	private EquipmentService epService;
	private JunyansService junyansService;
	private ICodeReviewService codeReviewService;
	public EquipmentService getEpService() {
		return epService;
	}

	public void setEpService(EquipmentService epService) {
		this.epService = epService;
	}

	public JunyansService getJunyansService() {
		return junyansService;
	}

	public void setJunyansService(JunyansService junyansService) {
		this.junyansService = junyansService;
	}

	public ICodeReviewService getCodeReviewService() {
		return codeReviewService;
	}

	public void setCodeReviewService(ICodeReviewService codeReviewService) {
		this.codeReviewService = codeReviewService;
	}

	/**
	 * 选择页面项目
	 */
	public void querySelectItem(){
		try{
			String junyansid = getRequest().getParameter("junyansid");
			String type = getRequest().getParameter("type");
			String xiangmumc = getRequest().getParameter("xiangmumc");
			Map<String,String> filterMap=getPagerMap();
			if(null!=junyansid && !"".equals(junyansid)){
				filterMap.put("junyansid", StringUtil.encodingUrl(junyansid));
			}
			if(null!=type && !"".equals(type)){
				filterMap.put("type", StringUtil.encodingUrl(type));
			}
			if(null!=xiangmumc && !"".equals(xiangmumc)){
				filterMap.put("xiangmumc", StringUtil.encodingUrl(xiangmumc));
			}
			PageResult result = null;
			if (type.equals("all")){
				result= junyansService.querySelectItemAll(filterMap);
			}
			else{
				result= junyansService.querySelectItemSelect(filterMap);
			}
			String[] viewArray={"ID","XIANGMUMC","BIAOZUNYQ","DANWEI","SHEBEIYQ"};
			String str=getColumnJson(result,viewArray);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("application/XML,charset=GBK");
			response.setHeader("Charset", "GBK");
			response.setCharacterEncoding("GBK");
			response.setContentType("text/xml");
			PrintWriter out = getResponse().getWriter();
			out.print(str);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
	}
	
	public void addItemTo(){
		try{
			String itemids = getRequest().getParameter("itemids");
			String junyansid = getRequest().getParameter("junyansid");
			String[] itemidarray = itemids.split(",");
			
			for(int i=0;i<itemidarray.length;i++){
				Junyansxm entity = new Junyansxm();
				entity.setJunyansid(Long.parseLong(junyansid));
				entity.setXiangmuid(Long.parseLong(itemidarray[i]));
				epService.saveObject(entity);
			}
			PrintWriter out = PubMethod.GetPrintWriter();
			out.print("{\"result\":\"success\"}");
		}
		catch(Exception exp){
			exp.printStackTrace();
		}
	}
	
	public void delItemTo(){
		try{
			String ids = getRequest().getParameter("ids");
			String[] idarray = ids.split(",");
			for(int i=0;i<idarray.length;i++){
				String strSql = "delete from kl_junyans_xm where id='"+idarray[i]+"' ";
				epService.exeSql(strSql);
			}
			PrintWriter out = PubMethod.GetPrintWriter();
			out.print("{\"result\":\"success\"}");
		}
		catch(Exception exp){
			exp.printStackTrace();
		}
	}
	
	public void checkYsjlReportPath(){
		String ids = getRequest().getParameter("ids");
		String sign = getRequest().getParameter("sign");
		String junyansid = getRequest().getParameter("junyansid");
		if (sign.equals("1")){
			String strSql = "select distinct t.ysjlpath from kl_junyans_xm t where t.id in ("+ids+")";
			List lists = codeReviewService.queryListBySql(strSql);
			PrintWriter out = PubMethod.GetPrintWriter();
			if (lists.size()>1){
				out.print("{\"result\":\"false\"}");
			}
			else{
				out.print("{\"result\":\"success\"}");
			}
		}
		else if (sign.equals("2")){
			String strSql = "select 1 from kl_junyans_xm t where junyansid='"+junyansid+"' and t.ysjlpath is null";
			List lists = codeReviewService.queryListBySql(strSql);
			strSql = "select t.reportpath from kl_junyans t where t.id='"+junyansid+"'";
			List junyanslist = codeReviewService.queryListBySql(strSql);
			PrintWriter out = PubMethod.GetPrintWriter();
			if (lists.size()>0||junyanslist.get(0)==null||junyanslist.get(0).equals("")){
				out.print("{\"result\":\"false\"}");
			}
			else{
				out.print("{\"result\":\"success\"}");
			}
		}
	}
	
	public void delYsjl(){
		PrintWriter out = PubMethod.GetPrintWriter();
		try{
			String ids = getRequest().getParameter("ids");
			String strSql = "update KL_JUNYANS_XM set JIEGUO=null,YSJLPATH=null where id in ("+ids+")";
			epService.exeSql(strSql);
			out.print("{\"result\":\"success\"}");
		}
		catch(Exception exp){
			exp.printStackTrace();
			out.print("{\"result\":\"error\"}");
		}
		
	}
	
	public void delReport(){
		PrintWriter out = PubMethod.GetPrintWriter();
		try{
			String ids = getRequest().getParameter("ids");
			String strSql = "update KL_JUNYANS_XM set REPORTPATH=null where id in ("+ids+")";
			epService.exeSql(strSql);
			out.print("{\"result\":\"success\"}");
		}
		catch(Exception exp){
			exp.printStackTrace();
			out.print("{\"result\":\"error\"}");
		}
		
	}

}
